<template>
    <div style="width:90%;" id="giftActivity">
        <el-form :model="planForm" :rules="planRules" ref="planForm" label-width="150px">
            <el-button @click="goback()" size="mini" type="primary" style="margin-left:1000px">返回</el-button>
            <el-input v-model="planForm.uuid" type="hidden"></el-input>

            <el-form-item label="活动名称：" prop="activityName">
                <el-input
                    v-model="planForm.activityName"
                    :disabled="disabledFlag"
                    style="width:360px;"
                    placeholder="活动名称"
                    maxlength="30"
                    show-word-limit
                ></el-input>
            </el-form-item>
            <el-form-item label="活动有效期：" prop="runTimeRange">
                <el-date-picker
                    v-model="planForm.runTimeRange"
                    type="daterange"
                    align="right"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :picker-options="pickerOptions"
                    :clearable="false"
                    :disabled="disabledFlag"
                ></el-date-picker>
            </el-form-item>

            <el-form-item label="赠品：" prop="planCouponList">
                <div class="giftTable">
                    <el-table
                        :data="planCouponList"
                        @selection-change="planCouponChecked"
                        border
                        style="font-size: 10px;width:90%;"
                        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                    >
                        <!-- <el-table-column type="selection" width="55" align="center" ></el-table-column> -->
                        <el-table-column prop="giftCode" label="赠品编码" align="center" width="150"></el-table-column>
                        <el-table-column prop="giftName" label="赠品名称" align="center" width="150"></el-table-column>
                        <el-table-column prop="giftPic" label="赠品图片" align="center" width="120">
                            <template slot-scope="scope">
                                <img
                                    :src="scope.row.giftPic"
                                    v-if="scope.row.giftPic !='' && scope.row.giftPic !=null"
                                    width="70"
                                    height="50"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="giftProperty"
                            label="赠品属性"
                            align="center"
                            width="120"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.giftProperty != '1' && scope.row.giftProperty != '2'"
                                >-</span>
                                <span v-if="scope.row.giftProperty == '1'">普通赠品</span>
                                <span v-if="scope.row.giftProperty == '2'">特殊赠品</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="giftDesc"
                            label="赠品描述"
                            align="center"
                            show-overflow-tooltip
                        ></el-table-column>
                        <el-table-column
                            label="赠品数量"
                            align="center"
                            width="250"
                            v-if="!disabledFlag"
                        >
                            <template slot-scope="scope">
                                <el-radio-group
                                    v-model="scope.row.isLimitQuantity"
                                    size="small"
                                    @change="changeIsLimitQuantity(scope.row,scope.$index)"
                                >
                                    >
                                    <el-radio-button label="1" class="el-radio-button-inner-child">
                                        <el-input
                                            type="number"
                                            :min="1"
                                            oninput="value=value.replace(/[^0-9]/g,'')"
                                            v-model="scope.row.giftCount"
                                            style="width:80px;text-align-last:right;"
                                            v-on:blur="giftSumQuantityCheck(scope.row,scope.$index)"
                                        ></el-input>
                                    </el-radio-button>
                                    <el-radio-button label="0">不限</el-radio-button>
                                </el-radio-group>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="赠品数量"
                            align="center"
                            width="150"
                            v-if="disabledFlag"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.isLimitQuantity == '1'"
                                >{{scope.row.giftCount}}</span>
                                <span v-if="scope.row.isLimitQuantity != '1'">不限</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="赠送数量" align="center" width="150">
                            <template slot-scope="scope">
                                <el-input
                                    type="number"
                                    :min="1"
                                    oninput="value=value.replace(/[^0-9]/g,'')"
                                    v-model="scope.row.giveCount"
                                    style="width:150px; text-align-last:right;"
                                    v-on:blur="giveCountCheck(scope.row,scope.$index)"
                                    :disabled="disabledFlag"
                                >
                                    <template slot="append">
                                        <span slot="append" v-if="scope.row.giftStyle ==1">件</span>
                                        <span slot="append" v-if="scope.row.giftStyle ==2">张</span>
                                        <span slot="append" v-if="scope.row.giftStyle ==3">次</span>
                                        <span slot="append" v-if="scope.row.giftStyle ==4">次</span>
                                        <span slot="append" v-if="scope.row.giftStyle ==5">积分</span>
                                    </template>
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="有效期类型" align="center" width="150">
                            <template slot-scope="scope">
                                <el-radio-group
                                    v-model="scope.row.validDateType"
                                    size="small"
                                    v-if="!disabledFlag && scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.giftStyle != '5'"
                                >
                                    <el-radio-button label="1">天数</el-radio-button>
                                    <el-radio-button label="2">日期</el-radio-button>
                                </el-radio-group>
                                <span
                                    v-if="disabledFlag && scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.giftStyle != '5' && scope.row.validDateType == '1'"
                                >天数</span>
                                <span
                                    v-if="disabledFlag && scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.giftStyle != '5' && scope.row.validDateType == '2'"
                                >日期</span>
                                <span
                                    v-if="scope.row.giftStyle == '1' || scope.row.giftStyle == '3' || scope.row.giftStyle == '5'"
                                >—</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="有效期" align="center" width="270">
                            <template slot-scope="scope">
                                <el-input
                                    v-if="scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.giftStyle != '5' && scope.row.validDateType == 1"
                                    type="number"
                                    :min="1"
                                    oninput="value=value.replace(/[^0-9]/g,'')"
                                    v-model="scope.row.dayCount"
                                    style="width:150px; text-align-last:right;"
                                    :disabled="disabledFlag"
                                >
                                    <template slot="append">天</template>
                                </el-input>
                                <el-date-picker
                                    v-if="scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.giftStyle != '5' && scope.row.validDateType == 2"
                                    v-model="scope.row.validDateRange"
                                    type="daterange"
                                    align="right"
                                    style="width:250px"
                                    unlink-panels
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    value-format="yyyy-MM-dd"
                                    :picker-options="pickerOptions"
                                    :clearable="false"
                                    :disabled="disabledFlag"
                                ></el-date-picker>
                                <span
                                    v-if="scope.row.giftStyle == '1' || scope.row.giftStyle == '3' || scope.row.giftStyle == '5'"
                                >—</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="status" label="状态" align="center" width="150">
                            <template slot-scope="scope">
                                <span v-if="scope.row.status == '1'">启用</span>
                                <span v-if="scope.row.status == '0'">弃用</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="操作"
                            fixed="right"
                            align="center"
                            width="200px"
                            v-if="!disabledFlag"
                        >
                            <template slot-scope="scope">
                                <el-button
                                    size="mini"
                                    @click="delprice(scope.$index)"
                                    type="text"
                                >删除赠品</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div style="margin-top:10px;">
                    <el-button
                        type="primary"
                        @click="selectCoupon('planForm')"
                        v-if="!disabledFlag"
                    >选择赠品</el-button>
                    <!-- <el-button @click="deleteCoupon('planForm')" v-if="!disabledFlag">删除赠品</el-button> -->
                </div>
            </el-form-item>
            <el-form-item label="发布状态：" prop="publishStatus">
                <el-radio-group v-model="planForm.publishStatus" :disabled="disabledFlag">
                    <el-radio-button label="0">不发布</el-radio-button>
                    <el-radio-button label="1">发布</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="活动描述：" prop="activityDesc">
                <el-input
                    type="textarea"
                    v-model="planForm.activityDesc"
                    :disabled="disabledFlag"
                    placeholder="请输入活动描述"
                    maxlength="120"
                    show-word-limit
                    :autosize="{minRows:2}"
                    style="width:650px;"
                ></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('planForm')" v-if="!disabledFlag">保 存</el-button>
                <el-button @click="resetForm('planForm')">取 消</el-button>
            </el-form-item>
        </el-form>

        <el-dialog title="选择赠品" :visible.sync="dialogVisibleCoupon" width="55%">
            <el-form
                :inline="true"
                :model="formCoupon"
                class="search-form-inline"
                style="margin-top:-20px;"
            >
                <el-form-item label="赠品编码：">
                    <el-input
                        v-model="formCoupon.giftCode"
                        clearable
                        placeholder
                        style="width:200px"
                    ></el-input>
                </el-form-item>
                <el-form-item label="赠品名称：">
                    <el-input
                        v-model="formCoupon.giftName"
                        clearable
                        placeholder
                        style="width:200px"
                    ></el-input>
                </el-form-item>
                <el-button
                    type="primary"
                    size="mini"
                    icon="el-icon-search"
                    @click="getCouponList()"
                >查询</el-button>
            </el-form>
            <el-table
                ref="singleTable"
                :data="couponList"
                @selection-change="couponChecked"
                border
                style="font-size: 10px;width:100%;"
                :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            >
                <el-table-column type="selection" width="60" align="center" fixed disabled></el-table-column>
                <el-table-column prop="giftPic" label="赠品图片" align="center" width="120">
                    <template slot-scope="scope">
                        <img
                            :src="scope.row.giftPic"
                            v-if="scope.row.giftPic !='' && scope.row.giftPic !=null"
                            width="70"
                            height="50"
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="giftType" label="赠品类型" align="center" width="120">
                    <template slot-scope="scope">
                        <span v-if="scope.row.giftType == '1'">实物赠品</span>
                        <span v-if="scope.row.giftType == '2'">虚拟赠品</span>
                        <span v-if="scope.row.giftType == '3'">虚拟赠品</span>
                    </template>
                </el-table-column>
                <el-table-column prop="giftProperty" label="赠品属性" align="center" width="120">
                    <template slot-scope="scope">
                        <span
                            v-if="scope.row.giftProperty != '1' && scope.row.giftProperty != '2'"
                        >-</span>
                        <span v-if="scope.row.giftProperty == '1'">普通赠品</span>
                        <span v-if="scope.row.giftProperty == '2'">特殊赠品</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="giftCode"
                    label="赠品编码"
                    align="center"
                    width="150"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="giftName"
                    label="赠品名称"
                    align="center"
                    width="200"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column prop="giftDesc" label="赠品描述" align="center" show-overflow-tooltip></el-table-column>
            </el-table>
            <div class="block" style="margin-top:22px;overflow:hidden">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :page-sizes="[5, 10, 20, 30, 50, 100]"
                    :current-page="listQuery.current"
                    :page-size="listQuery.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total3"
                ></el-pagination>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisibleCoupon = false">取 消</el-button>
                <el-button type="primary" @click="couponConfirm()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import {
  queryChooseGiftsList,
  saveGiftActivityWithGoods,
  qryShareActiviteInfo
} from "@/api/promotionProgramEdit";
import imageUpload from "@/page/common/Image-upload";
const curDateStr = function (){
  var curDate = new Date();
  var year = curDate.getFullYear();
  var month = curDate.getMonth() + 1;
  month = month > 9 ? month : "0" + month;
  var date = curDate.getDate();
  date = date > 9 ? date : "0" + date;
  return year + "-" + month + "-" + date;
}
export default {
  data() {
    return {
      keepAlive:true,
      logininfo: {},
      orgList: [],
    //   orgSeq: "",
      loading: true,
      typeOptions: [
              {value: "2",label: '虚拟赠品'},
              {value: "1",label: '实物赠品'}],
      lsPlanCouponList: [],
      lsCouponList: [],
      planCouponList: [],
      couponList: [],
      planForm: {
        uuid: "",
        giftType: "2",
        giftProperty:"1",
        activityName: "",
        activityDesc: "",
        // beginTime: "",
        // endTime: "",
        status: "",
        giftList: [],
        sharePic: "",
        invitedPic:"",
        giftId: "",
        publishStatus:"0",
          runTimeRange: [curDateStr(), curDateStr()],
      },
      formCoupon: {
        couponName: "",
        giftCode:"",
        giftName:"",
        giftType:""
      },
      dialogVisibleCoupon: false,
      disabledFlag: false,
      disabledFlagone: true,
      listQuery: {
        current: 1,
        size: 10
      },
      total3:0,
      total: 0,
            pickerOptions: {
        shortcuts: [
          {
            text: "未来3天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 3);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "未来一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "未来一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          }
        ],
        disabledDate(time){
          return time.getTime() < Date.now()-8.64e7   //如果当天可选，就不用减8.64e7
        }
      },
      planRules: {
       activityName: [{ required: true, message: "活动名称", trigger: "blur" }],
       publishStatus:[{ required: true, message: "请选择发布状态", trigger: "blur" }],
        giftType: [{ required: true, message: "请选择赠品类型", trigger: "blur" }],
        giftName: [{ required: true, message: "请输入赠品名称", trigger: "blur" }],
        activityDesc: [{ required: true, message: "请输入活动描述", trigger: "blur" }],
        publishStatus: [{ required: true, message: "请选择发布状态", trigger: "blur" }],
        planCouponList: [{ required: true,type: 'array', message: "请选择赠品", trigger: "change",
        validator: function (value) { return value.length !== 0}}],
     runTimeRange: [
          { required: true, message: "请选择活动有效期", trigger: "blur" }
        ]

      }
    };
  },
  components: {
        imageUpload
    },
  mounted() {
    this.logininfo = localStorage.getItem("logininfo");
    this.orgList = JSON.parse(this.logininfo).orgList;
    // this.orgSeq = this.orgList[0].orgSeq;
    // this.planForm.orgSeq = this.orgList[0].orgSeq;
this.disabledFlag=this.$route.params.disabledFlag
this.disabledFlagone=this.$route.params.disabledFlagone
    var v = this.$route.params.edit;
    if (v) {
      // 修改，回显值
      this.planForm.uuid = v.uuid;
      this.planForm.activityName = v.activityName;
      this.planForm.giftType = String(v.giftType);
       this.planForm.publishStatus = String(v.publishStatus);
        this.planForm.giftProperty = String(v.giftProperty);
      this.planForm.sharePic = v.sharePic;
       this.planForm.invitedPic = v.invitedPic;
      this.planForm.giftId = v.giftId;
      // this.planCouponList = res.result.promotionCouponInfo;
      this.planForm.activityDesc = v.activityDesc;
       this.planForm.runTimeRange = [v.beginDate, v.endDate];
    //   this.planForm.beginTime = v.beginTime;
    //   this.planForm.endTime = v.endTime;
      this.planForm.status = v.status;

      this.qryShareActiviteInfo(v.uuid);
    } else {
      // 新增，默认值
      this.planForm.uuid = "";
      this.planForm.status = "1";
      this.getCouponList();
    }
  },
  methods: {
    giveCountCheck(row, index) {
      let val = row.giveCount;
      if (val === null || val === undefined || val === "") {
        this.$message({showClose: true, message: "填入数量不能为空",type: "warning"});
        val = 1;
      } else if (isNaN(val)){
        this.$message({showClose: true, message: "填入数量格式错误",type: "warning"});
        val = 1;
      } else if (val < 1){
        this.$message({showClose: true, message: "填入数量应大于0",type: "warning"});
        val = 1;
      } else if( this.planCouponList[index].isLimitQuantity == '1' && val > this.planCouponList[index].giftCount){
        this.$message({showClose: true, message: "填入数不应大于赠送数",type: "warning"});
        val = this.planCouponList[index].giftCount;
      }
       this.planCouponList[index].giveCount = parseInt(val);
      return true;
    },
  //礼品总数量
    giftSumQuantityCheck(row, index) {
      let val = row.giftCount;
      if (val === null || val === undefined || val === "") {
        this.$message({showClose: true, message: "填入数量不能为空",type: "warning"});
        val = 1;
      } else if (isNaN(val)){
        this.$message({showClose: true, message: "填入数量格式错误",type: "warning"});
        val = 1;
      } else if (val < 1){
        this.$message({showClose: true, message: "填入数量应大于0",type: "warning"});
        val = 1;
      }
       this.planCouponList[index].giftCount = parseInt(val);
      this.planCouponList[index].isLimitQuantity = 1;
      if(val <this.planCouponList[index].giveCount){this.planCouponList[index].giveCount =1; }
      return true;
    },
      changeIsLimitQuantity(row, index){ 
      if(row.isLimitQuantity == 0){//不限
        this.planCouponList[index].giftCount = null;
      }else if(row.giftCount === null || row.giftCount === undefined || row.giftCount === ""){
          this.planCouponList[index].giftCount = 1;
      }
    },
         delprice(index) {
      this.planCouponList.splice(index, 1);
    },
    goback() {
      this.$router.go("-1");
    },
    quantityBlur(e, idx) {
      if (e.target.value == '') { return; }
      let flag = new RegExp("^[1-9][0-9]*$").test(e.target.value);
      if (!flag) {
        this.$message({
          message: "限领数量请输入正整数",
          type: "warning"
        });
        e.target.value = '';
      }
    },
    numberBlur(e, name) {
      if (e.target.value == "") {
        return;
      }
      let flag = new RegExp("^[1-9][0-9]*$").test(e.target.value);
      if (!flag) {
        this.$message({
          message: name + "请输入正整数",
          type: "warning"
        });
        e.target.value = "";
      }
    },
    qryShareActiviteInfo(uuid) {
      let data = {
     uuid:uuid
      };
      qryShareActiviteInfo(data).then(res => {

        // if (
        //   res.result.giftList!= null &&
        //   res.result.shareActivityGiftListlength > 0
        // ) {
            //  console.log(this.planCouponList,"op00");
          // this.planCouponList =res.records;
          // 去掉已选的
        for (var i in res.result.giftList) {

        //   if(uuid == res.result.giftList[i].uuid){
          this.planCouponList.push(res.result.giftList[i]);
        //   }
        }


         console.log(this.planCouponList,"opop09999")
        // }
        this.getCouponList();
      });
      this.loading = false;
    },

    getCouponList() {
      let data = {
        current: this.listQuery.current,
        size: this.listQuery.size,
        condition: {
        //   activityId: this.ruleForm.uuid,
        //   goodsId: this.editGoodsId,
          status: 1,
      activityType:3,
          giftName: this.formCoupon.giftName.trim(),
          giftCode: this.formCoupon.giftCode.trim(),
          giftType: this.formCoupon.giftType
        },
        ascs: [],
        descs: []
      };
      queryChooseGiftsList(data).then(res => {
        this.couponList = res.records;
        this.total3=res.total
        // 去掉已选的
        for (var i in this.planCouponList) {
          this.removeByValue(
            this.couponList,
            "uuid",
            this.planCouponList[i].uuid
          );
        }
      });
      this.loading = false;
    },

    planCouponChecked(row) {
        console.log("k")
      this.lsPlanCouponList = row;
    },
    couponChecked(row) {
      this.lsCouponList = row;

        console.log("k88")
    //   if (this.lsCouponList.length > 1) {
    //       this.$refs.singleTable.toggleRowSelection(this.lsCouponList[0]);
    //       }
    },
       handleSizeChange(val) {
      this.listQuery.size = val;
      this.getCouponList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getCouponList();
    },
    selectCoupon() {
      // 显示选择卡券对话框
      this.dialogVisibleCoupon = !this.dialogVisibleCoupon;
      this.getCouponList()
    },
    deleteCoupon() {
      // 删除已选择的卡券
      this.couponList = this.couponList.concat(this.lsPlanCouponList);
      for (var i in this.lsPlanCouponList) {
        this.removeByValue(
          this.planCouponList,
          "uuid",
          this.lsPlanCouponList[i].uuid
        );
      }
    },
    couponConfirm() {
      this.dialogVisibleCoupon = !this.dialogVisibleCoupon;
     //this.planCouponList.concat(this.lsCouponList);
      for (var i in this.lsCouponList) {
        this.$set(this.lsCouponList[i],"giveCount",1);
        this.$set(this.lsCouponList[i],"isLimitQuantity",0);
       this.planCouponList.push(this.lsCouponList[i]);
      }
    console.log(this.planCouponList,"op99")
     for (var i in this.planCouponList) {
     if (
          this.planCouponList[i].giftId == null ||
          this.planCouponList[i].giftId == undefined
        ) {
          this.planCouponList[i].giftId = this.planCouponList[i].uuid;
          this.planCouponList[i].uuid=null
        }
     }
    },

    submitForm(formName) {
      // 保存奖品

      if (this.planForm.activityName == "" || this.planForm.activityName == null) {
        this.$message.error("请输入活动名称");
        return;
       }
         if ( this.planCouponList.length == 0) {
        this.$message.error("请选择赠品");
        return;
      }
             if (this.planForm.publishStatus == "" || this.planForm.publishStatus == null) {
        this.$message.error("请选择发布状态");
        return;
       }
       if (this.planForm.activityDesc == "" || this.planForm.activityDesc == null) {
        this.$message.error("请输入活动描述");
        return;
       }

       let r = new RegExp("^[1-9][0-9]*$");
      for (var ii in this.planCouponList) { 
        var flag1 = true;
        var flag2 = true;
        var flag3 = true;
        let giftStyle = this.planCouponList[ii].giftStyle;
        let validDateType = this.planCouponList[ii].validDateType;
        let dayCount = this.planCouponList[ii].dayCount;
        let validDateRange = this.planCouponList[ii].validDateRange;
        if(giftStyle != '1' && giftStyle != '3' && giftStyle != '5'){
        if (validDateType == undefined) {
          flag1 = false;
          break;
        }
        }
        if(giftStyle != '1' && giftStyle != '3' && giftStyle != '5' && validDateType =='1'){
        if (dayCount == null || dayCount == "" || !r.test(dayCount)) {
          flag3 = false;
          break;
        }
        }
        if(giftStyle != '1' && giftStyle != '3' && giftStyle != '5' && validDateType =='2'){
        if (validDateRange == null || validDateRange.length == 0 || validDateRange[0] == '' ) {
          flag2 = false;
          break;
        }
        }
      }
      if (!flag1) {
        this.$message({ message: "请正确选择有效期类型按钮", type: "warning" }); return;
      }
      if (!flag2) {
        this.$message({ message: "请正确输入有效日期,且不能为空", type: "warning" }); return;
      }
      if (!flag3) {
        this.$message({ message: "请正确输入有效天数,且不能为空", type: "warning" }); return;
      }



      for (var i in this.planCouponList) {
        // 删除提交时不需要的属性或容易导致错误的属性
//         if(this.$route.params.disabledFlag!==false){
//  if (
//           this.planCouponList[i].uuid != null &&
//           this.planCouponList[i].uuid != undefined
//         ) {
//           this.planCouponList[i].giftId = this.planCouponList[i].uuid;
//           this.planCouponList[i].uuid=""
//         }
//         }

        delete this.planCouponList[i].createTime;
        delete this.planCouponList[i].createUser;
        delete this.planCouponList[i].updateTime;
        delete this.planCouponList[i].updateUser;
        // delete this.planCouponList[i].beginTime;
        // delete this.planCouponList[i].endTime;
        delete this.planCouponList[i].comments;
        delete this.planCouponList[i].validCreateDate;
        delete this.planCouponList[i].validEndDate;
      }
      this.planForm.giftList = this.planCouponList;
    //   if (this.planForm.giftType=="2"){
    //   this.planForm.giftId = this.planForm.giftList[0].uuid;
    //   this.planForm.giftName = this.planForm.giftList[0].giftName;
    //   }

       if(this.planForm.runTimeRange != null && this.planForm.runTimeRange.length == 2){
            this.planForm.beginDate = this.planForm.runTimeRange[0];
            this.planForm.endDate = this.planForm.runTimeRange[1];
          }
            for (var i in this.planCouponList) {
    //  if (
    //       this.planCouponList[i].giftId == null ||
    //       this.planCouponList[i].giftId == undefined
    //     ) {
        if(this.planCouponList[i].validDateType==2 ){
     this.planCouponList[i].validBeginDate = this.planCouponList[i].validDateRange[0];
        this.planCouponList[i].validEndDate = this.planCouponList[i].validDateRange[1];``
        }

        // }
     }

     this.keepAlive = false;

      console.log(this.planForm);
      if(this.planForm.uuid == ""){
          this.planForm.uuid = null
      saveGiftActivityWithGoods(this.planForm).then(res => {
        if (res.code == "1") {
          this.$message({
            showClose: true,
            message: "保存成功",
            type: "success"
          });
        //   this.$router.push("prizeLibrary");
        this.$router.push("promotionList");
        } else {
          this.$message({
            showClose: true,
            message: res.message,
            type: "error"
          });
        }
      });
      } else {
        saveGiftActivityWithGoods(this.planForm).then(res => {
        if (res.code == "1") {
          this.$message({
            showClose: true,
            message: "修改成功",
            type: "success"
          });
        //   this.$router.push("prizeLibrary");
        this.$router.push("promotionList");
        } else {
          this.$message({
            showClose: true,
            message: res.message,
            type: "error"
          });
        }
      });
      }
    },
    // 数组，属性，属性值
    removeByValue: function(arr, attr, value) {
      if (arr == null) {
        return;
      }
      var index = 0;
      for (var i in arr) {
        if (arr[i][attr] == value) {
          index = i;
          break;
        }
      }
      arr.splice(index, 1);
    },
    handleRemove(file, fileList) {
      let newfileList = [];
      fileList.forEach((item, i) => {
        let data = {
          name: item.name,
          url: item.url
        };
        newfileList.push(data);
      });
      this.fileList = newfileList;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$router.push("promotionList");
    }
  },
  beforeRouteLeave(to, from, next) {
    // 设置下一个路由的 meta
    to.meta.keepAlive = this.keepAlive; // C 跳转到 A 时让 A 不缓存，即刷新
    next();
  },
};
</script>
<style lang="less" scoped>
.el-col-20 {
    border: 1px solid #ebeef5;
    margin-left: 150px;
    width: 60%;
}
.el-input {
    width: 60%;
}
.search-form-inline {
    border: 1px solid #ebeef5;
    line-height: 30px;
    padding: 12px;
    margin-bottom: 5px;
}
thead {
    .el-table-column--selection {
        .cell {
            display: none;
        }
    }
}
#giftActivity
    .giftTable
    table
    .el-radio-button-inner-child
    .el-radio-button__inner {
    padding: 0;
}
#giftActivity .giftTable table .el-radio-button-inner-child .el-input__inner {
    border: none;
    height: 30px;
}
</style>
